<template>
  <div class="add-area">
    <div class="add-left">
      <el-icon>
        <Tickets />
      </el-icon>
      <span>数据列表</span>
    </div>
    <el-button type="primary" @click="" v-permission="'addUser'"
      >添加</el-button
    >
  </div>
  <el-table :data="list" border table-layout="auto" cell-class-name="align-c">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="菜单名称" prop="title" />
    <el-table-column label="菜单级数">
      <template #default>
        <span>一级</span>
      </template>
    </el-table-column>
    <el-table-column label="前端名称" prop="name" />
    <el-table-column label="前端图标" prop="icon">
      <template #default="{ row }">
        <i class="iconfont" :class="row.icon"></i>
      </template>
    </el-table-column>
    <el-table-column label="是否显示" prop="hidden">
      <template #default="{ row }">
        <el-switch
          v-model="row.hidden"
          :active-value="0"
          :inactive-value="1"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="排序" prop="sort" />
    <el-table-column label="设置">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="getUser(row.id)"
          v-permission="'editUser'"
          >查看下级</el-button
        >
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="editUser(row.id)"
          v-permission="'editUser'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import { Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import useTable from "@/hooks/useTable";
import { getMenuListApi, MenuUpdateHiddenApi } from "./api";
import { ElMessage } from "element-plus";
import router from "@/router";
const {
  list,
  pageNum,
  pageSize,
  total,
  pageChangeHandle,
  pageSizeChangeHandle,
} = useTable(getMenuListApi);

const editUser = (id: number) => {
  router.push("/ums/addMenu?id=" + id);
};

const getUser = (id: number) => {};

const switchStatus = (row: GetMenuList) => {
  MenuUpdateHiddenApi(row.id!, row.hidden)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("状态修改成功");
      } else {
        row.hidden = row.hidden === 0 ? 1 : 0;
      }
    })
    .catch(() => {
      row.hidden = row.hidden === 0 ? 1 : 0;
    });
};
</script>
<style lang="less" scoped>
:deep(.cell) {
  text-align: center !important;
}
.add-area {
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-bottom: 20px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  .add-left {
    display: flex;
    align-items: flex-end;
    span {
      padding-left: 10px;
      font-size: 14px;
    }
  }
}
.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
